#include "form_factory.h"
#include "ui_form_factory.h"

// add necessary includes here
#include "utils/configure/configure_bll.h"


/*
 * Function:    FormFactory
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormFactory::FormFactory(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormFactory)
{
    ui->setupUi(this);
}

/*
 * Function:    ~FormFactory
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormFactory::~FormFactory()
{
    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormFactory" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormFactory" class
 */
FormFactory* FormFactory::getInstance(QWidget *pParent)
{
    static FormFactory *pNew = nullptr;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormFactory(pParent);
        if(nullptr == pNew) return nullptr;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();
    }

    // Set the parent object
    if(pParent) pNew->setParent(pParent);

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormFactory::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    ui->pushButton_UvDisinfection->setText(" \n\n\n" + tr("UV disinfection"));
    ui->pushButton_ExtractDebug->setText(" \n\n\n" + tr("Extract Debug"));
    ui->pushButton_ProcessDebug->setText(" \n\n\n" + tr("Process Debug"));
    ui->pushButton_MotorDebug->setText(" \n\n\n" + tr("Motor Debug"));
    ui->pushButton_HeatingDebug->setText(" \n\n\n" + tr("Heating Debug"));
    ui->pushButton_FactoryParams->setText(" \n\n\n" + tr("Factory Params"));
    ui->pushButton_SelfChecking->setText(" \n\n\n" + tr("Self-checking"));
    ui->pushButton_Notify->setText(" \n\n\n" + tr("Device Notify"));
    ui->pushButton_MixingSpeed->setText(" \n\n\n" + tr("Mixing Speed"));

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormFactory::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/factory/%2.qss").arg("skin_1").arg(this->objectName()));

    // Set the property of buttons
    foreach (auto var, QList<QPushButton*>()
             << ui->pushButton_UvDisinfection
             << ui->pushButton_ExtractDebug
             << ui->pushButton_ProcessDebug
             << ui->pushButton_MotorDebug
             << ui->pushButton_HeatingDebug
             << ui->pushButton_FactoryParams
             << ui->pushButton_SelfChecking
             << ui->pushButton_Notify
             << ui->pushButton_MixingSpeed) {
        var->setFocusPolicy(Qt::NoFocus);
    }
}

void FormFactory::on_pushButton_UvDisinfection_clicked()
{
    // Show the UV disinfection window
    emit signalShowPageUvDisinfection();
}

void FormFactory::on_pushButton_ProcessDebug_clicked()
{
    // Show the process debug window
    emit signalShowPageProcessDebug();
}

void FormFactory::on_pushButton_ModuleCoding_clicked()
{
    // Show the module coding window
    emit signalShowPageModuleCoding();
}

void FormFactory::on_pushButton_MotorDebug_clicked()
{
    // Show the motor debug window
    emit signalShowPageMotorDebug();
}

void FormFactory::on_pushButton_HeatingDebug_clicked()
{
    // Show the heating debug window
    emit signalShowPageHeatingDebug();
}

void FormFactory::on_pushButton_ExtractDebug_clicked()
{
    // Show the extract debug window
    emit signalShowPageExtractDebug();
}

void FormFactory::on_pushButton_FactoryParams_clicked()
{
    // Show the factory params setting window
    emit signalShowPageFactoryParams(0);
}

void FormFactory::on_pushButton_SelfChecking_clicked()
{
    // Show the self-checking window
    emit signalShowPageSelfChecking();
}

void FormFactory::on_pushButton_Notify_clicked()
{
    // Show the device notify page
    emit signalShowPageDeviceNotify();
}

void FormFactory::on_pushButton_MixingSpeed_clicked()
{
    // Show the motor speed configuration page
    emit signalShowPageFactoryParams(1);
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormFactory::slotRefreshUI()
{
    ui->pushButton_Notify->setVisible(false);
    ui->pushButton_MotorDebug->setVisible(false);
    ui->pushButton_ProcessDebug->setVisible(false);
    ui->pushButton_HeatingDebug->setVisible(false);
    ui->pushButton_FactoryParams->setVisible(false);

    // Get the login mode
    ST_CFG_LOGIN_MODE stLoginMode;
    ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

    switch ((int)stLoginMode.eumLoginMode) {
    case E_MODE_FACTORY:
        ui->pushButton_Notify->setVisible(true);
        ui->pushButton_MotorDebug->setVisible(true);
        ui->pushButton_ProcessDebug->setVisible(true);
        ui->pushButton_HeatingDebug->setVisible(true);
        ui->pushButton_FactoryParams->setVisible(true);
        break;
    default:
        break;
    }
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *e - the pointer of "QEvent"
* Return:		void
*/
void FormFactory::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        qDebug() << Q_FUNC_INFO;
        ui->retranslateUi(this);

        ui->pushButton_UvDisinfection->setText(" \n\n\n" + tr("UV disinfection"));
        ui->pushButton_ExtractDebug->setText(" \n\n\n" + tr("Extract Debug"));
        ui->pushButton_ProcessDebug->setText(" \n\n\n" + tr("Process Debug"));
        ui->pushButton_MotorDebug->setText(" \n\n\n" + tr("Motor Debug"));
        ui->pushButton_HeatingDebug->setText(" \n\n\n" + tr("Heating Debug"));
        ui->pushButton_FactoryParams->setText(" \n\n\n" + tr("Factory Params"));
        ui->pushButton_SelfChecking->setText(" \n\n\n" + tr("Self-checking"));
        ui->pushButton_Notify->setText(" \n\n\n" + tr("Device Notify"));
        ui->pushButton_MixingSpeed->setText(" \n\n\n" + tr("Mixing Speed"));

        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}
